package top.jiangqiang.qyblog.core.config;

import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.jiangqiang.qyblog.common.id.IDGenerator;

/**
 * @author jiangqiang
 * @date 2022-12-19
 */
@Configuration
@RequiredArgsConstructor
public class MybatisConfiguration {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }

    /**
     * 使用注解@Lazy解决循环依赖问题 @Autowired @Lazy
     *
     * @param idGenerator id生成器
     * @return
     */
    @Bean
    public IdentifierGenerator identifierGenerator(IDGenerator idGenerator) {
        return new IdentifierGenerator() {
            @Override
            public Number nextId(Object entity) {
                return idGenerator.nextId();
            }

            @Override
            public String nextUUID(Object entity) {
                return idGenerator.nextUUID();
            }
        };
    }
}
